package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.DatInfo;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.mapper.DeviceMapper;
import com.ruoyi.system.service.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备Controller
 *
 * @author wxd
 * @date 2024-08-25
 */
@Api(value = "设备", tags = {"设备"})
@RestController
@RequestMapping("/system/device")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询设备列表
     */
    @ApiOperation("查询分页列表")
    @GetMapping("/list")
    public TableDataInfo list(Device device) {
        startPage();
        deviceMapper.expire();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation("查询字段分组列表")
    @Anonymous
    @GetMapping("/fieldGroup")
    public List<String> fieldGroup(String fieldName) {
        return deviceService.selectFieldGroup(fieldName);
    }

    /**
     * 导出设备列表
     */
    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Device device) {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(deviceService.selectDeviceById(id));
    }

    /**
     * 新增设备
     */
    @ApiOperation("新增")
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody Device device) {
        Device old = deviceService.selectDeviceById(device.getId());
        if(old!=null){
            return AjaxResult.error("已存在相同数据");
        }
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@RequestBody Device device) {
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(deviceService.deleteDeviceByIds(ids));
    }
}
